package com.yulongtian.offers.day202211.day20221108;

import java.util.Arrays;

/**
 * @author yulongTian
 * @create 2022-11-08 12:13
 */
public class Test02 {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5};//120, 60, 40, 30, 24
        int[] arr = constructArr(a);
        System.out.println(Arrays.toString(arr));
    }

    public static int[] constructArr(int[] a) {
        //left记录该数字左边所有数乘积
        //right记录该数字右边所有数乘积
        int left = 1, right = 1;
        int n = a.length;
        int[] ans = new int[n];
        Arrays.fill(ans, 1);

        for (int i = 0; i < n; i++) {
            //此次循环乘了左边乘积，同时对称的右半部乘了右边的连乘积
            //在循环到达n-x-1=i时该数乘了右边的连乘积，得到除自身以外的元素积
            ans[i] *= left;
            ans[n - i - 1] *= right;

            left *= a[i];
            right *= a[n - i - 1];
        }
        return ans;

    }

}
